package cn.tmpan.project.exception;

import cn.tmpan.project.common.ServerResponse;
import cn.tmpan.project.common.enums.ResponseCode;
import cn.tmpan.project.utils.RequestUtil;
import com.jpay.ext.kit.IpKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * The type Global exception controller advice.
 *
 * @Author tieMinPan
 * @Desc: Description 全局异常拦截 取消注入,会看不到错误日志!
 * @Date 2019 -05-08 10:56
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionControllerAdvice {

    /**
     * 全局异常处理，返回异常返回统一格式的map
     *
     * @param request   the request
     * @param exception the exception
     * @return server response
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ServerResponse exceptionHandler(HttpServletRequest request, Exception exception) {

        log.error("服务器内部错误: 请求地址--->{},请求ip--->:{},错误详情:{},请求参数:{}", request.getRequestURL(), IpKit.getRealIp(request), exception.getMessage(), RequestUtil.getParams(request));
        /**
         * 抛出异常 有利于日志排查
         */
        log.error("服务器内部请求异常:",exception);
        return ServerResponse.createByErrorCodeMessage(ResponseCode.EXCEPTION.getCode(), exception.getMessage());
    }
}
